using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace ViewOn.Helper.Security
{
	internal sealed class RegistryEncrypter
	{

		#region Variables

		private static string _encryptKey = "&%#@?,:*"; // For registry

		#endregion

		#region Cryptography

		internal static string Encrypt(string strText)
		{
			byte[] byKey = { };
			byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };

			try
			{
				byKey = Encoding.UTF8.GetBytes(_encryptKey.Substring(0, 8));
				DESCryptoServiceProvider des = new DESCryptoServiceProvider();
				byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
				MemoryStream ms = new MemoryStream();
				CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
				cs.Write(inputByteArray, 0, inputByteArray.Length);
				cs.FlushFinalBlock();
				return Convert.ToBase64String(ms.ToArray());
			}
			catch (Exception ex)
			{
				return ex.Message;
			}
		}

		internal static string Decrypt(string strText)
		{
			byte[] byKey = { };
			byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
			byte[] inputByteArray = new byte[strText.Length];

			try
			{
				byKey = Encoding.UTF8.GetBytes(_encryptKey.Substring(0, 8));
				DESCryptoServiceProvider des = new DESCryptoServiceProvider();
				inputByteArray = Convert.FromBase64String(strText);
				MemoryStream ms = new MemoryStream();
				CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
				cs.Write(inputByteArray, 0, inputByteArray.Length);
				cs.FlushFinalBlock();
				Encoding encoding = Encoding.UTF8;
				return encoding.GetString(ms.ToArray());
			}
			catch (Exception ex)
			{
				return ex.Message;
			}
		}

		#endregion

	}
}
